// import { message } from "antd";
import apis from "./pushCountApi";

export default {
  namespace: "pushCount",
  state: {
    appList: [],
    ruleList: [],
    dataList: [],
    filter: {
      pageSize: 10,
      pageIndex: 1
    }, // 过滤器，（分页数据缓存）
    totalCount: 0,
    totalPage: 0
  },
  effects: {
    // 下拉框应用数据获取
    *getAppList(empty, { call, put }) {
      const { data = [] } = yield call(apis.getAppList);
      yield put({ type: "set/appList", payload: data });
    },
    // 下拉框规则数据获取
    *getRuleList(empty, { call, put }) {
      const { data = [] } = yield call(apis.getRuleList);
      yield put({ type: "set/ruleList", payload: data });
    },
    // 下拉框查询数据获取
    *getDataList({ payload }, { select, call, put }) {
      const { filter } = yield select(state => state.pushCount);
      const params = { ...payload, ...filter };
      const { data = [], totalCount = 0, totalPage = 0 } = yield call(
        apis.getDataList,
        params
      );
      yield put({ type: "set/data", payload: { data, totalCount, totalPage } });
    }
  },
  reducers: {
    // 保存应用数据
    "set/appList": (state, { payload }) => ({
      ...state,
      appList: payload
    }),
    // 保存规则数据
    "set/ruleList": (state, { payload }) => ({
      ...state,
      ruleList: payload
    }),
    // 保存查询数据
    "set/data": (state, { payload }) => ({
      ...state,
      dataList: payload.data,
      totalCount: payload.totalCount,
      totalPage: payload.totalPage
    }),
    // 保存分页缓存数据
    "set/filter": (state, { payload }) => ({
      ...state,
      filter: payload
    })
  }
};
